/**
 * 定义路由映射关系
 */

import {createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router";
const routes:RouteRecordRaw[]=[
    {
        path:'/',
        redirect:'/news',
        //路由元信息
        meta:{
            title:'首页',
            needAuth:false
        }
    },
    {
        path:'/news',
        component:()=>import('../pages/News.vue'),
        meta:{
            title:'资讯',
            needAuth:false
        }
    },
    {
        path:'/detail',
        component:()=>import('../pages/Detail.vue'),
        meta:{
            title:'详情',
            needAuth:false
        }
    },
    {
        path:'/knowledge',
        component:()=>import('../pages/Knowledge.vue'),
        meta:{
            title:'知识库',
            needAuth:false
        }
    },
    {
        path:'/my',
        component:()=>import('../pages/My.vue'),
        meta:{
            title:'个人中心',
            needAuth:true
        }
    },
    {
        path:'/login',
        component:()=>import('../pages/Login.vue'),
        meta:{
            title:'登录',
            needAuth:false
        }
    },
    {
        path:'/register',
        component:()=>import('../pages/Register.vue'),
        meta:{
            title:'注册',
            needAuth:false
        }
    },
    {
        path:'/setting',
        component:()=>import('../pages/Setting.vue'),
        meta:{
            title:'设置',
            needAuth:true
        }
    },
    {
        path:'/password',
        component:()=>import('../pages/Password.vue'),
        meta:{
            title:'修改密码',
            needAuth:true
        }
    },
    {
        path:'/show/myexams',
        component:()=>import('../pages/Myexams.vue'),
        meta:{
            title:'我的考试',
            needAuth:true
        }
    },
    {
        path:'/show/examinfo/:id',
        component:()=>import('../pages/ExamInfo.vue'),
        meta:{
            title:'试卷详情',
            needAuth:true
        }
    },
    {
        path:'/show/collects',
        component:()=>import('../pages/MyCollect.vue'),
        meta:{
            title:'我的收藏',
            needAuth:true
        }
    },
    {
        path:'/show/collects',
        component:()=>import('../pages/MyCollect.vue'),
        meta:{
            title:'我的收藏',
            needAuth:true
        }
    },
    {
        path:'/page/list',
        component:()=>import('../pages/PageList.vue'),
        meta:{
            title:'试卷',
            needAuth:false
        }
    },
    {
        path:'/page/:id',
        component:()=>import('../pages/Page.vue'),
        meta:{
            title:'试卷',
            needAuth:true
        }
    }
]
const router = createRouter({
    history:createWebHashHistory(),
    routes
})
router.beforeEach((to,from,next)=>{
    document.title = to.meta.title
    let needAuth = to.meta.needAuth
    if (needAuth){
        //需要授权，需要登录后
        let token = localStorage.getItem('hsjyToken')
        if (token){
            //代表用户已经登录了的
            next()
        }else{
            next('/login')
        }
    }else{
        //不需要授权，直接访问
        next()
    }

})
export default router
